import pandas as pd
import numpy as np
from save import savepicture3, savepicture4
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import tkinter.messagebox
import tkinter
from tkinter import *
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from sklearn.tree import ExtraTreeRegressor


def predictinmap(ID):
    ID_list1 = []
    ID_list2 = []
    f = open('C:\\TEST\\python\\ID_list1.txt', 'r')
    x = f.readlines()
    f.close()
    for i in x:
        ID_list1.append(i.strip('\n'))

    f = open('C:\\TEST\\python\\ID_list2.txt', 'r')
    x = f.readlines()
    f.close()
    for i in x:
        ID_list2.append(i.strip('\n'))

    if ID not in ID_list1:
        tkinter.messagebox.showerror('警告', '输入的动物ID不存在!')

    elif ID not in ID_list2:
        tkinter.messagebox.showerror('警告', '输入的动物ID数据小于100条！')

    else:
        root1 = tkinter.Tk()
        root1.title('track of animal {}'.format(ID))
        path = 'C:\\TEST\\python\\grouped1\\{}.csv'.format(ID)
        df = pd.read_csv(path)
        s = df.shape[0]
        s1 = s - 100
        train = np.array([[df['location-long'][i], df['location-lat'][i]] for i in np.arange(s1)])
        test = np.array([[df['location-long'][i], df['location-lat'][i]] for i in np.arange(s1, s)])
        fig = plt.figure(figsize=(10, 10))
        map = Basemap(llcrnrlon=-10, llcrnrlat=-4, urcrnrlon=59, urcrnrlat=66, resolution='i',
                      area_thresh=5000.0, lat_0=0, lon_0=60)
        map.drawmapboundary(fill_color='aqua')  # 设置背景色
        map.fillcontinents(color='coral', lake_color='lightskyblue')  # 设置陆地颜色和湖的颜色
        map.drawcoastlines()  # 描出边界
        map.drawcountries(linewidth=0.25)  # 画出国境线
        x_train, y_train = train[:, :1], train[:, 1]
        x_test, y_test = test[:, :1], test[:, 1]
        linear = ExtraTreeRegressor()
        linear.fit(x_train, y_train)
        x, y = map(x_train, y_train)
        map.scatter(x, y, marker='o', color='g', zorder=40, s=10, label='train')  # 描出多个点的位置，marker为形状，color为颜色，
        plt.plot(x_train, linear.predict(x_train), color='yellow', linewidth=3, label='predict')
        plt.plot(x_test, linear.predict(x_test), 'or', markersize=6, label='test')
        plt.plot(x_test, y_test, '.b', label='in fact')
        plt.legend()
        canvas = FigureCanvasTkAgg(fig, master=root1)  # A tk.DrawingArea.
        canvas.draw()
        canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
        m = Menu(root1)
        m.add_command(label='保存图片', command=lambda: savepicture3(fig, ID))
        root1['menu'] = m
        root1.mainloop()
        plt.close()

def predictoutmap(ID):
    ID_list1 = []
    ID_list2 = []
    f = open('C:\\TEST\\python\\ID_list1.txt', 'r')
    x = f.readlines()
    f.close()
    for i in x:
        ID_list1.append(i.strip('\n'))

    f = open('C:\\TEST\\python\\ID_list2.txt', 'r')
    x = f.readlines()
    f.close()
    for i in x:
        ID_list2.append(i.strip('\n'))

    if ID not in ID_list1:
        tkinter.messagebox.showerror('警告', '输入的动物ID不存在!')

    elif ID not in ID_list2:
        tkinter.messagebox.showerror('警告', '输入的动物ID数据小于100条！')

    else:
        root1 = tkinter.Tk()
        root1.title('track of animal {}'.format(ID))
        path = 'C:\\TEST\\python\\grouped1\\{}.csv'.format(ID)
        df = pd.read_csv(path)
        s = df.shape[0]
        s1 = s - 100
        train = np.array([[df['location-long'][i], df['location-lat'][i]] for i in np.arange(s1)])
        test = np.array([[df['location-long'][i], df['location-lat'][i]] for i in np.arange(s1, s)])
        fig = plt.figure(figsize=(10, 10))
        x_train, y_train = train[:, :1], train[:, 1]
        x_test, y_test = test[:, :1], test[:, 1]
        linear = ExtraTreeRegressor()
        linear.fit(x_train, y_train)
        plt.scatter(x_train, y_train, color='green', label='train')
        plt.plot(x_train, linear.predict(x_train), color='yellow', linewidth=3, label='predict')
        plt.plot(x_test, linear.predict(x_test), 'or', markersize=6, label='test')
        plt.plot(x_test, y_test, '.b', label='in fact')
        plt.legend()
        canvas = FigureCanvasTkAgg(fig, master=root1)  # A tk.DrawingArea.
        canvas.draw()
        canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
        m = Menu(root1)
        m.add_command(label='保存图片', command=lambda: savepicture4(fig, ID))
        root1['menu'] = m
        root1.mainloop()
        plt.close()
